package com.tedu.springboot2302.controller;

import com.sun.net.httpserver.HttpServer;
import com.tedu.springboot2302.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/*
当前类用于处理与用户相关的业务操作
 */
@Controller
public class UserController {
    private static File userDir;

    static {
        userDir = new File("./users");
        if (!userDir.exists()) {
            userDir.mkdirs();
        }
    }


    @RequestMapping("/regUser")
    public void reg(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("开始处理用户注册!!!!!!!!!!!!!!!");
        //获取表单提交的信息
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String nickname = request.getParameter("nickname");
        String age = request.getParameter("age");
        System.out.println("用户名为：" + username);
        System.out.println("密码为：" + password);
        System.out.println("名字为：" + nickname);
        System.out.println("年龄为：" + age);

        //对浏览器提交的数据进行必要的验证
        if (username == null || username.isEmpty() ||
                password == null || password.isEmpty() ||
                nickname == null || nickname.isEmpty() ||
                age == null || age.isEmpty() ||
                !age.matches("[0-9]+")) {
            //直接给用于相应一个错误的提示页面

            try {
                response.sendRedirect("/reg_info_error.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }

        /**
         * 验证通过后，就要将该用户信息保存在服务端
         *
         * 将用户提交的注册信息以user实例形式表示
         * 然后将该user实例序列化到文件中做长久保存
         *
         *
         */

        int agenum = Integer.parseInt(age);
        User user = new User(username, password, nickname, agenum);
        //第一个参数的FIle对象表示当前文件在哪个目录中
        File file = new File(userDir, username + ".obj");


        //首先判断该文件是否已经存在了，存在则说明此为重复用户
        if (file.exists()) {
            try {
                response.sendRedirect("have_user.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }
        try (
                FileOutputStream fos = new FileOutputStream(file);
                ObjectOutputStream oos = new ObjectOutputStream(fos);
        ) {
            oos.writeObject(user);
            response.sendRedirect("/reg_success.html");
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    @RequestMapping("/loginUser")
    public void login(HttpServletRequest request, HttpServletResponse
            response) {

        System.out.println("开始处理用户登录");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if (username == null || username.isEmpty() ||
                password == null || password.isEmpty()) {
            try {
                response.sendRedirect("/login_info_error.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }


        File file = new File(userDir, username + ".obj");
        if (file.exists()) {
            try {
                FileInputStream fis = new FileInputStream(file);
                ObjectInputStream ois = new ObjectInputStream(fis);


                User user = (User) ois.readObject();
                if (user.getPassword().equals(password)) {
                    response.sendRedirect("/login_success.html");
                } else {
                    response.sendRedirect("/login_fail.html");
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

        } else {
            try {
                response.sendRedirect("login_fail.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}








